package org.dromara.system.controller;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.PlanSum;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.system.domain.bo.AppManyProcurementPlanBo;
import org.dromara.system.domain.bo.TotalPlanBo;
import org.dromara.system.domain.vo.TotalPlanVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.AppProcurementPlanVo;
import org.dromara.system.domain.bo.AppProcurementPlanBo;
import org.dromara.system.service.IAppProcurementPlanService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 采购计划
 *
 * @author Lion Li
 * @date 2025-09-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/procurementPlan")
public class AppProcurementPlanController extends BaseController {

    private final IAppProcurementPlanService appProcurementPlanService;

    /**
     * 公司汇总
     */
    @SaCheckPermission("system:procurementPlan:list")
    @PostMapping("/total")
    public R<TotalPlanVo> total(@RequestBody TotalPlanBo totalPlanBo) {
        return R.ok(appProcurementPlanService.total(totalPlanBo));
    }


    /**
     * 查询采购计划列表
     *  司机  type  1->抢单大厅  2->待确认承运
     *  仓库管理员  type  3->送达待入库  10->已退费
     *  质检员  type  4->待验订单  5->质检完成订单
     *  财务员  type  6->待结算  7->已结算
     *  供应商  type  11->待核算 12->已核算
     */
    @SaCheckPermission("system:procurementPlan:list")
    @GetMapping("/list")
    public TableDataInfo<AppProcurementPlanVo> list(AppProcurementPlanBo bo, PageQuery pageQuery) {
        return appProcurementPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出采购计划列表
     */
    @SaCheckPermission("system:procurementPlan:export")
    @Log(title = "采购计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppProcurementPlanBo bo, HttpServletResponse response) {
        List<AppProcurementPlanVo> list = appProcurementPlanService.queryList(bo);
        ExcelUtil.exportExcel(list, "采购计划", AppProcurementPlanVo.class, response);
    }

    /**
     * 获取采购计划详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:procurementPlan:query")
    @GetMapping("/{id}")
    public R<AppProcurementPlanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appProcurementPlanService.queryById(id));
    }

    /**
     * 新增采购计划
     */
    @SaCheckPermission("system:procurementPlan:add")
    @Log(title = "采购计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppManyProcurementPlanBo bo) {
        //采购计划具有层级关系 如果是年度计划 则下级是季度 如果是季度下级是月 如果是月下级是日

        //获取今年的日期

        if (CollUtil.isNotEmpty(bo.getAppProcurementPlanBos())){
            for (AppProcurementPlanBo planItem : bo.getAppProcurementPlanBos()) {



                String fPlanCodeId = appProcurementPlanService.insertByBo(planItem);
                if (CollUtil.isNotEmpty(bo.getAppProcurementPlanBos())){
                    if (fPlanCodeId!=null){
                        List<AppProcurementPlanBo> appProcurementPlanBos = planItem.getAppProcurementPlanBos();
                        if (CollUtil.isNotEmpty(appProcurementPlanBos)){
                            for (AppProcurementPlanBo appProcurementPlanBo : appProcurementPlanBos) {
                                appProcurementPlanBo.setPid(fPlanCodeId);
                                appProcurementPlanService.insertByBo(appProcurementPlanBo);
                            }
                        }
                    }
                }
            }
        }


        return toAjax(1);
    }

    /**
     * 修改采购计划
     */
    @SaCheckPermission("system:procurementPlan:edit")
    @Log(title = "采购计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppProcurementPlanBo bo) {
        return toAjax(appProcurementPlanService.updateByBo(bo));
    }

    /**
     * 删除采购计划
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:procurementPlan:remove")
    @Log(title = "采购计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(appProcurementPlanService.deleteWithValidByIds(List.of(ids), true));
    }
}
